//
//  CWQButtonWithBadge.m
//  MobileClinicalOnline
//
//  Created by 陈武琦 on 16/8/8.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "CWQ_NoticeButton.h"

@implementation CWQ_NoticeButton

- (instancetype)initWithFrame:(CGRect)frame image:(NSString *)image type:(CWQ_NoticeButtonType)type
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor blueColor];
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [_btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
       
        [_btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        [self addSubview:_btn];
        
        _badge = [[UILabel alloc] init];
        _badge.backgroundColor = [UIColor redColor];
        _badge.hidden = NO;
        _badge.layer.borderColor = [UIColor whiteColor].CGColor;
        _badge.layer.masksToBounds = YES;
        [self addSubview:_badge];
        self.type = type;
    }
    return self;
}



- (void)setType:(CWQ_NoticeButtonType)type {

    _type = type;
    switch (type) {
        case CWQ_NoticeButtonWithNumberType:{
            [_badge setFrame:CGRectMake(self.frame.size.width -15, 0, 15, 15)];
            _badge.textAlignment = NSTextAlignmentCenter;
            _badge.textColor = [UIColor whiteColor];
            _badge.adjustsFontSizeToFitWidth = YES;
        }
            break;
        case CWQ_NoticeButtonWithRedDotType:{
            
            [_badge setFrame:CGRectMake(self.frame.size.width -10, 5, 5, 5)];
        }
            break;
            
        default:
            break;
    }
    _badge.layer.cornerRadius = _badge.frame.size.width/2.0;

}



- (void)click:(UIButton *)sender {
    if (_clickBlock) {
        self.clickBlock();
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
